/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ["admin", "editor"];
  return valid_map.indexOf(str.trim()) >= 0;
}

export function validPhone(str) {
  const reg = /^1\d{10}$/;
  return reg.test(str);
}

// 验证密码
// 密码长度6-20位，必须包含数字、必须包含大写或小写字母的其中一种
export function validPassword(str) {
  const reg = /^(?=.*\d)(?=.*[a-z])[0-9a-zA-Z]{6,20}$/;

  const reg1 = /^(?=.*\d)(?=.*[A-Z])[0-9a-zA-Z]{6,20}$/;
  return reg.test(str) || reg1.test(str);
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg =
    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
  return reg.test(url);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/;
  return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/;
  return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z0-9]{6,20}$/;
  return reg.test(str);
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg =
    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return reg.test(email);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === "string" || str instanceof String) {
    return true;
  }
  return false;
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === "undefined") {
    return Object.prototype.toString.call(arg) === "[object Array]";
  }
  return Array.isArray(arg);
}
export function getDateTime(date_time) {
  const today = new Date();
  const date = new Date(date_time);
  if (
    date.getFullYear() === today.getFullYear() &&
    date.getMonth() === today.getMonth() &&
    date.getDate() === today.getDate()
  )
    return (
      (date.getHours() > 9 ? date.getHours() : "0" + date.getHours()) +
      ":" +
      (date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes()) +
      ":" +
      (date.getSeconds() > 9 ? date.getSeconds() : "0" + date.getSeconds())
    );
  else {
    if (date.getFullYear() !== today.getFullYear()) {
      return `${date.getFullYear() + 1}年${
        date.getMonth() + 1
      }${date.getDate()}日`;
    } else return `${date.getMonth() + 1}月${date.getDate()}日`;
  }
}

export function getFormatTime(timestamp) {
  // 将秒转换为毫秒
  let date = new Date(timestamp * 1000);
  // 格式化日期
  let year = date.getFullYear(); // 年
  let month = String(date.getMonth() + 1).padStart(2, "0"); // 月（注意：月份从0开始，所以要加1）
  let day = String(date.getDate()).padStart(2, "0"); // 日
  let hours = String(date.getHours()).padStart(2, "0"); // 时
  let minutes = String(date.getMinutes()).padStart(2, "0"); // 分
  let seconds = String(date.getSeconds()).padStart(2, "0"); // 秒
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

export function toParse(val) {
  const getParse = (str) => {
    try {
      return JSON.parse(str);
    } catch (e) {
      return str;
    }
  };
  const value = getParse(val);
  if (typeof value === "object") {
    Object.keys(value).map((key) => {
      value[key] = getParse(value[key]);
      if (typeof value[key] === "object") {
        Object.keys(value[key]).map((key2) => {
          value[key][key2] = getParse(value[key][key2]);
        });
      }
    });
  }
  return value;
}

// 允许整数或最多两位小数
export function isAllNumber(value) {
  const reg = /^\d+(\.\d{1,2})?$/;
  // 处理类似 "123." 的情况（最后带小数点视为无效）
  return reg.test(value) && !/\.$/.test(value);
}

export function getToday(num) {
  if (num === 0) return "今天";
  if (num === 1) return "明天";
  if (num === 3) return "后天";
  else return num + "天";
}

// 传入正整数，以当前时间 + 传入的天数为基准，返回格式化后的日期字符串
export function getDateStr(num) {
  const dateTime = new Date().getTime();
  const nextDate = new Date(dateTime + num * 24 * 60 * 60 * 1000);
  const year = nextDate.getFullYear();
  const month = nextDate.getMonth() + 1;
  const date = nextDate.getDate();
  return `${year}/${month}/${date}`;
}
